//
//  SDHomeProductData.m
//
//  Created by   on 2018/11/15
//  Copyright (c) 2018 __MyCompanyName__. All rights reserved.
//

#import "SDHomeProductData.h"
#import "SDHomeProductList.h"


NSString *const kSDHomeProductDataTypeName = @"typeName";
NSString *const kSDHomeProductDataId = @"id";
NSString *const kSDHomeProductDataList = @"list";


@interface SDHomeProductData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SDHomeProductData

@synthesize typeName = _typeName;
@synthesize id = _id;
@synthesize list = _list;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.typeName = [self objectOrNilForKey:kSDHomeProductDataTypeName fromDictionary:dict];
            self.id = [[self objectOrNilForKey:kSDHomeProductDataId fromDictionary:dict] doubleValue];
    NSObject *receivedSDHomeProductList = [dict objectForKey:kSDHomeProductDataList];
    NSMutableArray *parsedSDHomeProductList = [NSMutableArray array];
    
    if ([receivedSDHomeProductList isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSDHomeProductList) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSDHomeProductList addObject:[SDHomeProductList modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSDHomeProductList isKindOfClass:[NSDictionary class]]) {
       [parsedSDHomeProductList addObject:[SDHomeProductList modelObjectWithDictionary:(NSDictionary *)receivedSDHomeProductList]];
    }

    self.list = [NSArray arrayWithArray:parsedSDHomeProductList];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.typeName forKey:kSDHomeProductDataTypeName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.id] forKey:kSDHomeProductDataId];
    NSMutableArray *tempArrayForList = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.list) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForList] forKey:kSDHomeProductDataList];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.typeName = [aDecoder decodeObjectForKey:kSDHomeProductDataTypeName];
    self.id = [aDecoder decodeDoubleForKey:kSDHomeProductDataId];
    self.list = [aDecoder decodeObjectForKey:kSDHomeProductDataList];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_typeName forKey:kSDHomeProductDataTypeName];
    [aCoder encodeDouble:_id forKey:kSDHomeProductDataId];
    [aCoder encodeObject:_list forKey:kSDHomeProductDataList];
}

- (id)copyWithZone:(NSZone *)zone {
    SDHomeProductData *copy = [[SDHomeProductData alloc] init];
    
    
    
    if (copy) {

        copy.typeName = [self.typeName copyWithZone:zone];
        copy.id = self.id;
        copy.list = [self.list copyWithZone:zone];
    }
    
    return copy;
}


@end
